package com.tedsoft.first_group_project;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

//import android.util.Log;

import com.tedsoft.framework.*;


public class GameField 
{
	List<Item> items;
	List<ItemBackgroundStar> stars;
	int maxX, maxY, yBottomMargin;
	int curX; //aktualne polozenie 'kamery'
	
	void generate_field(int field_width, int field_difficulty, Game game)
	{
		Random generator = new Random();
		
		Graphics g = game.getGraphics();
		
		maxX = field_width;
		maxY = Settings.gameFieldHeight;
		yBottomMargin = Settings.gameFieldBottomMargin;
		curX = 0;
	
		items = new ArrayList<Item>();
		stars = new ArrayList<ItemBackgroundStar>();
		
		int x, y, z, id;
		e_item_type temp_type;
		
		//tutaj wygenerujemy mapke
		final int rand_range = 60; 
		final int starting_free_passage = 300; 
		
		if(field_difficulty == 0)
		{
			for(int i=starting_free_passage; i < maxX; i+=rand_range)
				for(int j=0; j < 2; j++)
				{
					id =  generator.nextInt( 6 ) + 1;
				
					x = generator.nextInt( rand_range ) + i;			
					y = generator.nextInt( maxY ) + (g.getHeight() - maxY - yBottomMargin);
					
					temp_type = e_item_type.enemy;				
					if(j == 0)
					{
						temp_type = e_item_type.point;
						id =  generator.nextInt( 3 ) + 1;
					}

					Item temp_item = new Item(temp_type, id, x, y);
					
					//zeby itemy nie wychodzily poza plansze gry (poza dolna krawedz)
					if(temp_item.posY + temp_item.height > (g.getHeight() - yBottomMargin))
						temp_item.posY = (g.getHeight() - yBottomMargin) - temp_item.height; 
					
					//tutaj kontrola zeby generowane elementy na siebie nie wchodzily
					boolean new_item_colided = true;
					Item check_temp_item;
					while(new_item_colided == true && items.size()>0)
					{
						for(int k=0; k<items.size(); k++)
						{
							check_temp_item = items.get(k);
							if(getItemsCollisions(temp_item, check_temp_item))
							{
								//element nachodzi na inny
								//przeuwamy go w lewo (nie jest to zbyt losowe ale narazie niech zostanie bo dziala:P)
								temp_item.posX -= 2;
								
								// odnawiamy petle, tak zeby po przesunieciu itema sprwadzila go z cala reszta
								new_item_colided = true;
								break;
							}
							else
								new_item_colided = false;
						}
					}		
							
					items.add(temp_item);
				}		
			//posortujemy sobie to teraz rosnaco
			Collections.sort(items, new ItemComparatorPosX());
		}
		
		//generowanie tla z gwiazd	
		int gridX=50, gridY=30, howMany;
		for(int i=0; i < maxX; i+=gridX)
			for(int j=0; j < maxY; j+=gridY)
			{
				howMany = generator.nextInt( 2 );
				for(int k=0; k<howMany; k++)
				{
					x = generator.nextInt( gridX ) + i;
					
					y = generator.nextInt( gridY ) + j + (g.getHeight() - maxY - yBottomMargin);
					
					z = generator.nextInt( 4 ) + 2;
				
					ItemBackgroundStar temp_item = new ItemBackgroundStar(x, y, z);
					
					stars.add(temp_item);
				}
			}
		Collections.sort(items, new ItemComparatorPosX());
	}		
		 
	void update(float deltaTime, int player_cur_x)
	{
		curX = player_cur_x - Settings.ship_left_margin;
	}
	
	void present(float deltaTime, Game game)
	{
		Item temp_item;
		ItemBackgroundStar temp_star;
		Graphics g = game.getGraphics();
		int maxW = g.getWidth();
    	maxW += curX;
  	
		//czyscimy ekran
    	int black_color = Color.convert(0, 0, 0, 255);    	
       	g.clear(black_color);
        
    	//rysujemy biale kreski ograniczajace pole gry
    	//int white_color = Color.convert(255, 255, 255, 255);
		//g.drawRect(0, (g.getHeight() - maxY - yBottomMargin), maxW+1, 3, white_color);
		//g.drawRect(0, (g.getHeight() - yBottomMargin), maxW+1, 3, white_color);
		
		//rysujemy gwiazdki
		//int background_star_color = Color.convert(32, 94, 193, 255); - ten kolor bardzo fajny - taki ciemniejszy
		//int background_star_color = Color.convert(57, 109, 193, 255);
		int background_star_color = Color.convert(150, 109, 193, 255);
		for(int i=0; i < stars.size(); i++)
		{
			//to jest zajebiscie nieoptymalny sposob ale niech bedzie narazie
			temp_star = stars.get(i);

			if((temp_star.posX / temp_star.posZ) >= (curX/temp_star.posZ - 50) && (temp_star.posX / temp_star.posZ) <= maxW)
			{
				int temp_size = 2;
				g.drawRect((temp_star.posX / temp_star.posZ) - curX/temp_star.posZ, temp_star.posY, temp_size, temp_size, background_star_color);				
			}
		}
				
		//rysujemy itemy
		for(int i=0; i < items.size(); i++)
		{
			//to jest zajebiscie nieoptymalny sposob ale niech bedzie narazie
			temp_item = items.get(i);
			
			//item powinien byc na ekranie
			if(temp_item.posX >= (curX - 50) && temp_item.posX <= maxW);
			{
				Pixmap temp_pixmap;
					//RYSOWANIE METEOROW
				if(temp_item.type == e_item_type.enemy)
				{
					if(temp_item.id == 1)
						temp_pixmap = Assets.meteor_1;
					else if(temp_item.id == 2)
						temp_pixmap = Assets.meteor_2;
					else if(temp_item.id == 3)
						temp_pixmap = Assets.meteor_3;
					else if(temp_item.id == 4)
						temp_pixmap = Assets.meteor_4;
					else if(temp_item.id == 5)
						temp_pixmap = Assets.meteor_5;
					else if(temp_item.id == 6)
						temp_pixmap = Assets.meteor_6;
					/*
					else if(temp_item.id == 7)
						temp_pixmap = Assets.meteor_7;
					else if(temp_item.id == 8)
						temp_pixmap = Assets.meteor_8;
					*/
					else
						temp_pixmap = Assets.ship_sprite;//jak bedzie jakies dziwne id to niech narysuje samolot, wykryje sie bagza latwiej;d
					
					g.drawPixmap(temp_pixmap, temp_item.posX - curX, temp_item.posY, 0, 0,  temp_item.width, temp_item.height);	
					
					//g.drawRect(temp_item.posX - curX, temp_item.posY, temp_item.width, temp_item.height, enemy_color);
				}
				else if(temp_item.type == e_item_type.point)
				{
					if(temp_item.id == 1)
						temp_pixmap = Assets.resource_1;
					else if(temp_item.id == 2)
						temp_pixmap = Assets.resource_2;
					else if(temp_item.id == 3)
						temp_pixmap = Assets.resource_3;
					else
						temp_pixmap = Assets.ship_sprite;//jak bedzie jakies dziwne id to niech narysuje samolot, wykryje sie bagza latwiej;d
					
					g.drawPixmap(temp_pixmap, temp_item.posX - curX, temp_item.posY, 0, 0,  temp_item.width, temp_item.height);	
					
					//g.drawRect(temp_item.posX - curX, temp_item.posY, temp_item.width, temp_item.height, score_color);				
					//g.drawPixmap(Assets.resource_1, temp_item.posX - curX, temp_item.posY, 0, 0,  temp_item.width, temp_item.height);
					
				}
			}
		}
	}	 
	
    public boolean inBounds(int x1, int y1, int x, int y, int width, int height)
    {
        if(x1 > x && x1 < x + width - 1 && 
        	y1 > y && y1 < y + height - 1) 
            return true;
        else
            return false;
    }	
    
	boolean getItemsCollisions(Item i1, Item i2)
	{
		//srodek
		if(inBounds(i1.posX + i1.width/2, i1.posY + i1.height/2, i2.posX, i2.posY, i2.width, i2.height))
			return true;
		//lewy gorny rog
		else if (inBounds(i1.posX, i1.posY, i2.posX, i2.posY, i2.width, i2.height))
			return true;
		//lewy dolny rog
		else if (inBounds(i1.posX, i1.posY + i1.height, i2.posX, i2.posY, i2.width, i2.height))
			return true;
		//prawy gorny rog
		else if (inBounds(i1.posX + i1.width, i1.posY, i2.posX, i2.posY, i2.width, i2.height))
			return true;	
		//prawy dolny rog
		else if (inBounds(i1.posX + i1.width, i1.posY + i1.height, i2.posX, i2.posY, i2.width, i2.height))
			return true;			
		else
			return false;
	}    
}
